package kr.co.choongang;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

@WebServlet("/Upload")
@MultipartConfig(maxFileSize=1024*1024*2, location="c:\\upload")
public class UploadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private String getFilename(Part part)
	{
		String fileName = null;
		String contentDispositionHeader = part.getHeader("content-disposition");
		String [] elements = contentDispositionHeader.split(";");
		for(String element : elements)
		{
			if( element.trim().startsWith("filename"))
			{
				fileName = element.substring(element.indexOf('=') + 1);
				fileName = fileName.trim().replace("\"", "");
			}
		}
		
		return fileName;
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");
		
		Part part = request.getPart("theFile");
		String fileName = getFilename(part);
		if( fileName != null && !fileName.isEmpty())
		{
			part.write(fileName);
		}
		
		String author = request.getParameter("theAuthor");
		author = new String( author.getBytes("iso-8859-1"), "UTF-8");
		
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		out.print("작성자:" + author + "<br>");
		//out.print("파일명:" + fileName + "<br>");
		out.print("파일명:<a href='FileDown?file_name=" + fileName + "'>" + fileName + "</a><br>");
		out.print("파일크기:" + part.getSize() + "<br>");
	}

}
